/**
 * @filename:FlowLogController May 12, 2023
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2020 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Date;
import java.util.Map;

import com.wonder.app.domain.FlowLog;
import com.wonder.app.vo.FlowLogVo;
import com.wonder.app.service.FlowLogService;
import com.wonder.app.request.FlowLogRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.utils.StringUtils;
import com.wonder.utils.DBUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
/**   
 * 
 * <p>说明： 资金流水API接口层</P>
 * @version: 1.1.0
 * @author: Wonder
 * @time    May 12, 2023
 *
 */
@Api(tags = "资金流水")
@RestController
@RequestMapping("/app/FlowLog")
public class FlowLogController {
	
	@Autowired
	private FlowLogService flowLogService;
	
	@Autowired
	private DBUtils dbUtils;
	
	@ApiOperation(value = "资金流水列表")
	@RequestMapping(value = "/index", method = RequestMethod.POST)
	public Map<String,Object> index(@RequestBody FlowLogRequest flowLogRequest) {
	    Page<FlowLogVo> page = new Page<FlowLogVo>(flowLogRequest.getPage(), flowLogRequest.getLimit());
	    IPage<FlowLogVo> list = flowLogService.selectFlowLogVoList(flowLogRequest, page);
		
		Map<String,Object> retMap=new HashMap<>();
		retMap.put("data",CommonPage.restPage(list));
		
        Map<String,Object> smallTable=new HashMap<>();
        smallTable.put("types",dbUtils.getDropDownList("select id,name from sn_flow_type"));
        smallTable.put("user_callers",dbUtils.getDropDownList("select user_id,name from sys_admin where is_caller=1"));
        retMap.put("sql_field_data",smallTable);
  
		return AjaxMap.success(retMap);
	}
	
	/**
	 * @explain 获取sql语句定义的字段信息
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/getFieldList")
	@ApiOperation(value = "获取sql语句定义的字段信息", notes = "作者：Wonder")
	public AjaxResult<Map<String,Object>> getFieldList(){
	    Map<String,Object> smallTable=new HashMap<>();
        smallTable.put("types",dbUtils.getDropDownList("select id,name from sn_flow_type"));
        smallTable.put("user_callers",dbUtils.getDropDownList("select user_id,name from sys_admin where is_caller=1"));
        return AjaxResult.success(smallTable);
	}
	
	/**
	 * @explain 修改排序开关
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/updateExt")
	@ApiOperation(value = "修改排序开关", notes = "作者：Wonder")
	public AjaxResult<FlowLog> updateExt(@RequestBody FlowLog entity){
		if (null!=entity) {
			boolean rsg = flowLogService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success("system.updateSuccess");
			  }else {
				  return AjaxResult.failed("system.updateFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 添加
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/add")
	@ApiOperation(value = "添加", notes = "作者：Wonder")
	public AjaxResult<FlowLog> add(@RequestBody FlowLog entity){

		if (null!=entity) {
		    
		    String validate=flowLogService.validate(entity,"new");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);    
			boolean rsg = flowLogService.save(entity);
			if (rsg) {
				  return AjaxResult.success("system.insertSuccess");
			  }else {
				  return AjaxResult.failed("system.insertFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 查看对象明细  
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/detail")
	@ApiOperation(value = "获取资金流水对象明细", notes = "作者：Wonder")
	public AjaxResult<FlowLogVo> detail(@RequestBody Map<String,Object> map){
	    if(map==null) return AjaxResult.failed("system.parameterError");
		Long id=Long.parseLong(map.get("id").toString());		
	    FlowLogVo flowLog=flowLogService.getVoById(id);
		if (null!=flowLog ) {	
			 return AjaxResult.success(flowLog);
		}else {
			 return AjaxResult.failed("system.parameterError");
		}
		
	}
	
	
}